# -*- coding: utf-8 -*-
# !/usr/bin/env python
from components.models import ConfManagement
from utils.mysql_utils import query_to_list


def query_soft_info(soft_id=''):
    sql_parameter = []

    if soft_id:
        sql_fragment = "WHERE a.soft_id = %s"
        sql_parameter.append(soft_id)
    else:
        sql_fragment = ''

    sql = """
    SELECT *
    FROM soft_management a
    %s
    AND a.status != 0
    """%sql_fragment
    data = query_to_list(sql, sql_parameter, True)

    return data

def get_install_conf(conf_manage_ids='', type=""):
    sql_parameter = []

    if conf_manage_ids:
        sql_fragment = "WHERE a.conf_manage_ids = %s"
        sql_parameter.append(conf_manage_ids)
    else:
        sql_fragment = ''

    if type == "app":
        sql_fragment_t = 'AND b.sys_code_type = "app_conf_manage_type"'
        sql_parameter.append(conf_manage_ids)
    else:
        sql_fragment_t = 'AND b.sys_code_type = "soft_install_conf_type"'

    sql = """
    SELECT a.* FROM conf_management a, sys_code b
    %s
    AND a.status != 0
    AND a.conf_manage_code = b.sys_code
    %s
    """%(sql_fragment, sql_fragment_t)
    data = query_to_list(sql, sql_parameter)

    return data

def post_install_conf(install_conf, conf_manage_ids):
    sql_update_list = []
    for for_list_one in install_conf:
        for_list_one['conf_manage_ids'] = conf_manage_ids
        for_list_one['status'] = 1
        obj = ConfManagement(**for_list_one)
        sql_update_list.append(obj)
    ConfManagement.objects.bulk_create(sql_update_list)

    return True


def update_install_conf(install_conf):
    for for_list_one in install_conf:
        ConfManagement.objects.filter(conf_manage_id=for_list_one['conf_manage_id']).update(
            conf_manage_content=for_list_one['conf_manage_content'],
            conf_manage_path=for_list_one['conf_manage_path']
        )
    return True
